package top.fyukina.fymil.elements;

import org.dom4j.Element;
import top.fyukina.fymil.exceptions.NoSuchCmdException;
import java.util.List;

public class CmdBlock {
    private final List<Element> cmds;
    private final VarDomainStack varDomainStack;
    public CmdBlock(Element blockEl, VarDomainStack varDomainStack) {
        cmds = blockEl.elements();
        this.varDomainStack = varDomainStack;
    }
    public void execute(Environment env) {
        varDomainStack.createVarDomain();
        //设置程序上下文全局变量
        varDomainStack.saveVar("$path", System.getProperty("user.dir"), true);
        for (Element cmd : cmds) {
            //TODO 局部异常处理器实现
            try {
                env.reg().cmdParser.parse(cmd, env);
            } catch (NoSuchCmdException ignored) {
            } catch (Exception e) {
                e.printStackTrace();
            }
        }
        varDomainStack.eraseVarDomain();
    }
}